package com.qh.system.controller;

import com.qh.basic.util.AjaxResult;
import com.qh.basic.util.pageList;
import com.qh.system.domain.DictionaryDetail;
import com.qh.system.query.DictionaryDetailQuery;
import com.qh.system.service.DictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典明细接口类",description = "处理数据字典明细的crud操作")
@RestController//==@reponseBody+@Controller
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {

    @Autowired
    private DictionaryDetailService dictionaryDetailService;

    /**
     * 查找单个
     * @param id
     * @return
     */
    //http://localhost:8080/dictionaryDetail/1
    @ApiOperation("查询单个的接口")
    @GetMapping("/{id}")
    public DictionaryDetail findOne(@PathVariable("id")Long id){
        return dictionaryDetailService.loadOne(id);
    }

    /**
     * 查找所有
     * @return
     */
    //http://localhost:8080/dictionaryDetail
    @ApiOperation("查询所有的")
    @GetMapping
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //http://localhost:8080/dictionaryDetail/1
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            dictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或者修改
     * @param dictionaryDetail
     * @return
     */
    //http://localhost:8080/dictionaryDetail/json对象
    @ApiOperation("添加或者修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null){
                dictionaryDetailService.save(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页
     * @param dictionaryDetailQuery
     * @return
     */
    //http://localhost:8080/dictionaryDetail/json对象
    @ApiOperation("分页")
    @PostMapping
    public pageList pageQuery(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.pageQuery(dictionaryDetailQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("接口：批量删除")
    @PatchMapping
    public AjaxResult patch(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
}
